package com.xywh.micro_warehousing.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jess.arms.base.BaseConstants;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.xywh.micro_warehousing.R;
import com.xywh.micro_warehousing.di.component.DaggerTab5Component;
import com.xywh.micro_warehousing.di.module.Tab5Module;
import com.xywh.micro_warehousing.mvp.contract.Tab5Contract;
import com.xywh.micro_warehousing.mvp.presenter.Tab5Presenter;
import com.xywh.micro_warehousing.mvp.ui.entity.ModifyPassword;
import com.xywh.micro_warehousing.mvp.ui.entity.ModifyPhone;
import com.xywh.micro_warehousing.mvp.ui.entity.Tab5;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.NiceSpinner;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class Tab5Fragment extends BaseFragment<Tab5Presenter> implements Tab5Contract.View {


    @BindView(R.id.rl_face_recognition)
    RelativeLayout rlFaceRecognition;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_password)
    TextView tvPassword;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_modify_phone)
    TextView tvModifyPhone;
    @BindView(R.id.niceSpinner)
    NiceSpinner niceSpinner;

    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    public static Tab5Fragment newInstance() {
        Tab5Fragment fragment = new Tab5Fragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerTab5Component //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .tab5Module(new Tab5Module(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab5, container, false);
        initToolbar(view);
        return view;
    }

    private void initToolbar(View view) {
        ToolbarUtils.initToolbarTitleNoBack(view, this, "我的");
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        tvUsername.setText(mSharedPreferencesHelper.getString(BaseConstants.USERNAME));
        tvPhone.setText(mSharedPreferencesHelper.getString(BaseConstants.MOBILE_PHONE));
        //getTab5Data();


        ArrayList<String> list = new ArrayList<String>();
        list.add("部门1");
        list.add("部门2");
        list.add("部门3");
        list.add("部门4");

        niceSpinner.attachDataSource(list);
        niceSpinner.setGravity(Gravity.LEFT);
        niceSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)}
     * 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     *
     * <p>
     * Example usage:
     *
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void getTab5DataSuccess(Tab5 entityData) {


    }

    @Override
    public void modifyPasswordSuccess(ModifyPassword entityData) {

    }

    @Override
    public void modifyPhoneSuccess(ModifyPhone entityData) {

        if (entityData != null && entityData.getCode() == 200) {

            showMessage("电话修改成功");
            killMyself();
        } else {
            showMessage("电话修改失败");
        }


    }

    private void getTab5Data() {
        HashMap<String, String> map = new HashMap<>();
        mPresenter.getTab5Data(map);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(getActivity(), message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @OnClick(R.id.rl_face_recognition)
    public void onViewClicked() {
        Utils.navigation(getActivity(), RouterHub.ACTIVITY_FACE_RECOGNITION);

    }

    @OnClick({R.id.tv_password, R.id.tv_modify_phone, R.id.ll_yu_liu, R.id.ll_chu_ru_ku_ji_lu, R.id.ll_cha_kan_ku_cun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_password:
//                showModefyPwdNegativeDialog();
                Utils.navigation(getActivity(), RouterHub.ACTIVITY_MODIFY_PWD);

                break;
            case R.id.tv_modify_phone:
                showModefyPhoneNegativeDialog();
                break;
            case R.id.ll_yu_liu:
                Utils.navigation(getActivity(), RouterHub.ACTIVITY_YU_LIU_JI_LU_LIST);
                break;
            case R.id.ll_chu_ru_ku_ji_lu:
                Utils.navigation(getActivity(), RouterHub.ACTIVITY_CHU_RU_KU_JI_LU);
                break;
            case R.id.ll_cha_kan_ku_cun:
                Utils.navigation(getActivity(), RouterHub.ACTIVITY_SAO_MIAO_CHA_KAN_KU_CUN);
                break;
        }
    }

    private void showModefyPhoneNegativeDialog() {

        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle("确认修改电话")
                .setPlaceholder("在此输入您的电话")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
//                            Toast.makeText(getActivity(), "您的昵称: " + text, Toast.LENGTH_SHORT).show();
                            HashMap<String, String> map = new HashMap<>();
                            map.put("phone", text.toString().trim());
                            mPresenter.modifyPhone(map);
                            tvPhone.setText(text);
                            dialog.dismiss();
                        } else {
                            showMessage("请填入电话");
                        }
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    private void showModefyPwdNegativeDialog() {

        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle("确认修改密码")
                .setPlaceholder("在此输入您的旧密码")
                .setPlaceholder("在此输入您的新密码")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            Toast.makeText(getActivity(), "您的昵称: " + text, Toast.LENGTH_SHORT).show();
//                            HashMap<String, String> map = new HashMap<>();
//                            map.put("oldPassword", "oldPassword");
//                            map.put("newPassword", "newPassword");
//                            mPresenter.modifyPassword(map);
                            dialog.dismiss();
                        } else {
                            showMessage("请填入密码");
                        }
                    }
                })
                .create(mCurrentDialogStyle).show();
    }
}
